from jsonrpclib.SimpleJSONRPCServer import PooledJSONRPCServer
from jsonrpclib.threadpool import ThreadPool

def add(a: int, b: int) -> int:
    import time
    time.sleep(3)
    return a + b

notify_pool = ThreadPool(max_threads=10, min_threads=0)
request_pool = ThreadPool(max_threads=50, min_threads=10)

notify_pool.start()
request_pool.start()

server = PooledJSONRPCServer(("localhost", 8080), thread_pool=request_pool)
server.set_notification_pool(notify_pool)
# server.register_function(lambda x, y: x + y, 'add')
server.register_function(add)

try:
    server.serve_forever()
finally:
    request_pool.stop()
    notify_pool.stop()
    server.set_notification_pool(None)